package com.yxinsur.imc.web.controller.common;

import com.yxinsur.imc.common.core.domain.AjaxResult;
import com.yxinsur.imc.common.core.domain.model.LoginUser;
import com.yxinsur.imc.common.utils.ServletUtils;
import com.yxinsur.imc.common.utils.file.OSSClientUtil;
import com.yxinsur.imc.framework.web.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件上传控制器
 */
@Api(value = "文件上传公共服务" , tags = {"9.文件上传公共服务"})
@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private TokenService tokenService;

    /**
     * 文件上传
     */
    @ApiOperation("上传资源，返回链接")
    @PostMapping("/upload")
    public AjaxResult avatar(@RequestParam("file") List<MultipartFile> files) throws IOException {
        if (!files.isEmpty()) {
            ArrayList<String> urls = new ArrayList<>();
            for (MultipartFile file : files) {
                String url = OSSClientUtil.upload2OSS(file);
                urls.add(url);
            }
            return AjaxResult.successData(urls);
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }

}
